#if UNITY_IOS
using UnityEditor;
using UnityEditor.Callbacks;
using UnityEditor.iOS.Xcode;

namespace UnityXEditor
{
    public class WeiXinBuild
    {
        [PostProcessBuild]
        public static void ProcessBuild(BuildTarget target, string path)
        {
            switch (target)
            {
                case BuildTarget.Android:
                    AndroidBuild(path);
                    break;
                case BuildTarget.iOS:
                    iOSBuild(path);
                    break;
                default:
                    break;
            }
        }
        public static void AndroidBuild(string path)
        {

        }
        public static void iOSBuild(string path)
        {
            //修改Info.plist文件
            PlistDocument plist = new PlistDocument();
            plist.ReadFromFile(path + "/Info.plist");
            {
                //出口合规证明不使用加密
                plist.root.SetBoolean("ITSAppUsesNonExemptEncryption", false);
                //APP语言
                plist.root.SetString("CFBundleDevelopmentRegion", "zh-Hans");
                //添加权限
                plist.root.SetString("NSPhotoLibraryUsageDescription", "保存拍照和录像");
                //添加白名单
                PlistElementArray queriesSchemes = plist.root.CreateArray("LSApplicationQueriesSchemes");
                queriesSchemes.AddString("weixin");
                queriesSchemes.AddString("weixinULAPI");
                queriesSchemes.AddString("weixinURLParamsAPI");
                //添加微信AppId
                PlistElementArray urlTypes = plist.root.CreateArray("CFBundleURLTypes");
                var urlItem2 = urlTypes.AddDict();
                urlItem2.SetString("CFBundleTypeRole", "Editor");
                urlItem2.SetString("CFBundleURLName", "weixin");
                var schemes2 = urlItem2.CreateArray("CFBundleURLSchemes");
                schemes2.AddString(WeChat.AppId);
            }
            plist.WriteToFile(path + "/Info.plist");

            //修改工程配置文件
            string pbxPath = PBXProject.GetPBXProjectPath(path);
            PBXProject project = new PBXProject();
            project.ReadFromFile(pbxPath);
            {
                string mainGuid = project.GetUnityMainTargetGuid();
                //添加Framework
                project.AddFrameworkToProject(mainGuid, "Security.framework", true);
                project.AddFrameworkToProject(mainGuid, "CoreGraphics.framework", true);
                project.AddFrameworkToProject(mainGuid, "WebKit.framework", true);
                //添加Other Linker Flags "-ObjC -all_load"
                project.AddBuildProperty(mainGuid, "OTHER_LDFLAGS", "-ObjC");
                project.AddBuildProperty(mainGuid, "OTHER_LDFLAGS", "-all_load");
                //添加"applinks:www.starpainters.cn"
                //project.AddCapability(mainGuid, PBXCapabilityType.AssociatedDomains);

                string frameworkGuid = project.GetUnityFrameworkTargetGuid();
                //添加Framework
                project.AddFrameworkToProject(frameworkGuid, "Security.framework", true);
                project.AddFrameworkToProject(frameworkGuid, "CoreGraphics.framework", true);
                project.AddFrameworkToProject(frameworkGuid, "WebKit.framework", true);
            }
            project.WriteToFile(pbxPath);
            
            //ProjectCapabilityManager capabilityManager = new ProjectCapabilityManager(pbxPath, "Entitlements.entitlements", null, project.GetUnityMainTargetGuid());
            ProjectCapabilityManager capabilityManager = new ProjectCapabilityManager(pbxPath, "Unity-iPhone/Unity-iPhone.entitlements", null, project.GetUnityMainTargetGuid());
            capabilityManager.AddAssociatedDomains(new string[] { "applinks:www.starpainters.cn" });
            capabilityManager.AddSignInWithApple();
            capabilityManager.WriteToFile();
        }
    }
}
#endif